package com.qzw.security.service.entity;

import com.qzw.common.service.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Getter
@Setter
@ToString
@Entity
@DynamicInsert
@DynamicUpdate
public class SysMenu extends BaseEntity<Integer> {

    private String name;
    private String description;
    private String url;
    @Column
    private Integer parentId;
    @ColumnDefault("0")
    @Column(nullable = false)
    private Integer sort;
    @ColumnDefault("1")
    @Column(nullable = false)
    private Byte status;
    @ColumnDefault("1")
    @Column(nullable = false)
    private Byte type;
    @Column(nullable = false, updatable = false)
    @CreationTimestamp
    private Date createTime;

    @OneToMany(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "parentId")
    private Set<SysMenu> children;

    @ManyToMany(mappedBy = "menus")
    private Set<SysRole> roles;

    @ManyToMany
    @JoinTable(
            name = "sys_menu_permission",
            joinColumns = @JoinColumn(name = "menu_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "permission_id", referencedColumnName = "id")
    )
    private Set<SysPermission> permissions;
}
